// pages/schedule/schedule.js

import API from "../../data/api";
import Util from "../../utils/util";

const App = getApp();
const Config = App.Config;

Page({
  data: {
    Stnumber: null,
    Stpassword: null,
    HOSTIMG: Config.HOSTIMG,
    clickScheduleIcon: false,
    selecTerm: 0,
    arrayTerm: ['第一周', '第二周', '第三周', '第四周', '第五周', '第六周', '第七周', '第八周', '第九周', '第十周', '第十一周', '第十二周', '第十三周', '第十四周', '第十五周', '第十六周', '第十七周', '第十八周', '第十九周', '第二十周'],
    demoData: [
      { "code": "1-2", "text1": "", "text2": "", "text3": "", "text4": "", "text5": "", "text6": "", "text7": "" },
      { "code": "3-4", "text1": "", "text2": "", "text3": "", "text4": "", "text5": "", "text6": "", "text7": "" },
      { "code": "5-6", "text1": "", "text2": "", "text3": "", "text4": "", "text5": "", "text6": "", "text7": "" },
      { "code": "7-8", "text1": "", "text2": "", "text3": "", "text4": "", "text5": "", "text6": "", "text7": "" },
      { "code": "9-10", "text1": "", "text2": "", "text3": "", "text4": "", "text5": "", "text6": "", "text7": "" },
      { "code": "11-12", "text1": "", "text2": "", "text3": "", "text4": "", "text5": "", "text6": "", "text7": "" },
      { "code": "13-14", "text1": "", "text2": "", "text3": "", "text4": "", "text5": "", "text6": "", "text7": "" },
    ],
    listData: [],
    nowWeek: '',
    color: ["blue", "green", "red", "orange", "yellow", "purple", "gray", "pink"],
    colorCourse: [''],
  },


  onShow: function () {
    var that = this;
    if (App.globalData.openId == '' || App.globalData.openId == null) {
      Util.UI.loading(1000, '授权失败,请退出小程序重新登录');
      setTimeout(function () {
        wx.switchTab({
          url: '../index/index',
        })
      }, 1000)
    } else {
      App.Util.getStNumberPassword(App.globalData.openId) // 获取学号密码
        .then((res) => {
          // console.log(res.data.number);
          return App.Util.checkBind(res);  // 判断是否已经绑定
        })
        .then((res) => {
          // console.log(res);

          that.setData({
            Stnumber: res.data.number,
            Stpassword: res.data.password,
          });

          // 获取课表
          that.getSchedule(-1);

        })

    }

  },


  // 点击选择周次图标
  clickSchedule: function () {
    var that = this;
    if (that.data.clickScheduleIcon == true) {
      this.setData({
        clickScheduleIcon: false,
      })
    } else {
      this.setData({
        clickScheduleIcon: true,
      })
    }
  },

  // 点击选择周次
  pickerTermChange: function (e) {
    var that = this;
    this.setData({
      selecTerm: e.detail.value,
      clickScheduleIcon: false,
    })
    // console.log(parseInt(e.detail.value) + 1);
    that.getSchedule(parseInt(e.detail.value) + 1);
  },

  // 获取课表
  getSchedule: function (week) {
    var that = this;

    API.getSchedule(week, that.data.Stnumber, that.data.Stpassword)
      .then((res) => {
        // console.log(res);
        if (res.data.error == Config.ERR.OK) {

          // 处理课表
          var newData = that.data.demoData;
          var i, j, k = 0, colorNu = 0;
          for (i = 0; i < newData.length; i++) {
            for (j = 1; j <= 7; j++) {
              // console.log(newData[i]['text'+j]);
              // console.log(k);
              if (res.data.data[k].course) {

                // console.log(res.data[0].data.data[k].course.courseName.length);

                // 截取课程名字
                var elCourseName = null;
                if (res.data.data[k].course.courseName.length > 5) {
                  elCourseName = res.data.data[k].course.courseName.substring(0, 4) + '..';
                } else {
                  elCourseName = res.data.data[k].course.courseName;
                }
                res.data.data[k].course['elCourseName'] = elCourseName;

                // 颜色块
                if (that.data.colorCourse[res.data.data[k].course.courseName]) {
                  res.data.data[k].course['color'] = that.data.colorCourse[res.data.data[k].course.courseName];
                } else {
                  if (colorNu > 7) {
                    colorNu = 0;
                  }
                  var newColorCourse = that.data.colorCourse;
                  newColorCourse[res.data.data[k].course.courseName] = that.data.color[colorNu];
                  that.setData({ colorCourse: newColorCourse });
                  res.data.data[k].course['color'] = that.data.color[colorNu];
                  colorNu++;
                }


                //处理相同课程
                res.data.data[k].course['show'] = true;
                if (k >= 7 && res.data.data[k - 7].course && (res.data.data[k].course.courseName == res.data.data[k - 7].course.courseName)) {
                  res.data.data[k].course['show'] = false;
                }

                newData[i]['text' + j] = res.data.data[k].course;
                // console.log(newData[i]['text' + j]);
              }
              k++;
            }
          }
          var nowWeek = parseInt(res.data.data.nowWeek) - 1;
          that.setData({
            demoData: [
              { "code": "1-2", "text1": "", "text2": "", "text3": "", "text4": "", "text5": "", "text6": "", "text7": "" },
              { "code": "3-4", "text1": "", "text2": "", "text3": "", "text4": "", "text5": "", "text6": "", "text7": "" },
              { "code": "5-6", "text1": "", "text2": "", "text3": "", "text4": "", "text5": "", "text6": "", "text7": "" },
              { "code": "7-8", "text1": "", "text2": "", "text3": "", "text4": "", "text5": "", "text6": "", "text7": "" },
              { "code": "9-10", "text1": "", "text2": "", "text3": "", "text4": "", "text5": "", "text6": "", "text7": "" },
              { "code": "11-12", "text1": "", "text2": "", "text3": "", "text4": "", "text5": "", "text6": "", "text7": "" },
              { "code": "13-14", "text1": "", "text2": "", "text3": "", "text4": "", "text5": "", "text6": "", "text7": "" },
            ],
            listData: newData,
            selecTerm: nowWeek
          });
        } else if (res.data.error == Config.ERR.ER) {
          Util.UI.loading(1000, res.data.data);
        }
      });
  },



  // 获取课表详细信息
  clickTd: function (event) {
    // console.log(event);
    if (event.currentTarget.dataset.info == "") {
      return;
    }
    wx.showModal({
      title: '详细内容',
      content: event.currentTarget.dataset.info.courseName + ' / ' + event.currentTarget.dataset.info.room + ' / ' + event.currentTarget.dataset.info.teacherName + ' / ' + event.currentTarget.dataset.info.studyWeek,
      showCancel: false,
    })
  },




})

// 生成随机数  0,5  (0-4)
function random(min, max) {
  return Math.floor(min + Math.random() * (max - min));
}
